package com.recyclerviewdemo;

import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.header.TwoLevelHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import static com.facebook.react.bridge.UiThreadUtil.runOnUiThread;

/**
 * Created by apple on 2018/6/14.
 */

public class RecyclerViewManager extends SimpleViewManager<SmartRefreshLayout> {

    private RecyclerView recyclerView;
    private SmartRefreshLayout smartRefreshLayout;
    private HomeAdapter homeAdapter;
    private ArrayList list;

    @Override
    public String getName() {
        return "RCTRecyclerView";
    }

    @Override
    protected SmartRefreshLayout createViewInstance(ThemedReactContext reactContext) {
        ViewGroup.LayoutParams framelayout_params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        FrameLayout frameLayout = new FrameLayout(reactContext);
        frameLayout.setLayoutParams(framelayout_params);

        initData();


        smartRefreshLayout = (SmartRefreshLayout) LayoutInflater.from(reactContext).inflate(R.layout.native_layout, frameLayout, false);
        smartRefreshLayout.setRefreshHeader(new ClassicsHeader(reactContext));

        recyclerView = smartRefreshLayout.findViewById(R.id.recycle_view);

        recyclerView.setLayoutManager(new LinearLayoutManager(reactContext));

        homeAdapter = new HomeAdapter(R.layout.recyclerview_item, list);

//        TextView tv = new TextView(reactContext);
//        tv.setText("我是头布局");
//
//        homeAdapter.addHeaderView(tv);
        recyclerView.setAdapter(homeAdapter);

//        frameLayout.addView(smartRefreshLayout);

        setListener();
        return smartRefreshLayout;
    }

    private void setListener() {
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initData();
                        homeAdapter.setNewData(list);
                        refreshLayout.finishRefresh();
                    }
                }, 2000);
            }
        });

        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {

                recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        int size = list.size();
                        List<String> newList = new ArrayList<String>();
                        for (int i = size; i < size + 15; ++i) {
                            newList.add("" + i);
                            list.add("" + i);
                        }
                        homeAdapter.addData(newList);

                        if (list.size() > 50) {
                            homeAdapter.loadMoreEnd();
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        } else {
                            smartRefreshLayout.finishLoadMore();
                        }


                    }
                }, 2000);




            }
        });


    }

    private void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < 15; ++i) {
            list.add("" + i);
        }
    }
}
